package edu.nf.project.services.storeOperation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.nf.project.entitys.entity.storeOperation.convention.inspect.InspectImage;
import edu.nf.project.entitys.entity.storeOperation.convention.inspect.SelfInspection;
import edu.nf.project.mappers.storeOperation.convention.InspectImageMapper;
import edu.nf.project.services.storeOperation.InspectImageService;
import edu.nf.project.services.storeOperation.SelfInspectionService;
import edu.nf.project.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author CSY
 * @date 2025 - 06 - 15
 */
@Service
public class InspectImageServiceImpl implements InspectImageService {
    private static Integer size = 0;

    @Autowired
    private InspectImageMapper inspectImageMapper;

    @Autowired
    private SelfInspectionService selfInspectionService;
    @Override
    public List<InspectImage> listInspectImageBySiId(Integer siId) {
        LambdaQueryWrapper<InspectImage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InspectImage::getSiId, siId);
        return inspectImageMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertInspectImage(MultipartFile[] files,String inspectImagesJson) {
        if (inspectImagesJson != null && !inspectImagesJson.isEmpty()) {
            size++;
            try {
                // 解析inspectImagesJson为InspectImage
                ObjectMapper objectMapper = new ObjectMapper();
                InspectImage inspectImage = objectMapper.readValue(
                        inspectImagesJson,
                        new TypeReference<>() {}
                );
                //图片上传 - 由于前端是逐个上传的
                String imageName = uploadImage(files);
                //设置图片路径
                inspectImage.setImage(imageName);
                //将图片信息存入数据库
                inspectImageMapper.insert(inspectImage);
                if (size == 4){
                    //修改自检状态
                    SelfInspection selfInspection = new SelfInspection();
                    selfInspection.setSiId(inspectImage.getSiId());
                    selfInspection.setSiStatus(true);
                    selfInspectionService.updateSelfInspection(selfInspection);
                    size = 0;
                }
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public String uploadImage(MultipartFile[] files) {
        String imageName = "";
        for (MultipartFile file : files) {
            imageName = FileUploadUtil.fileUpload(file);
        }
        return imageName;
    }
}
